package de.baumann.browser.views.tab;

import android.content.Context;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

import de.baumann.browser.R;
import de.baumann.browser.adapter.UCTabAdapter;
import de.baumann.browser.web.WindowUiController;

/**
 * @author：
 * @date：2018/11/5
 * @discription: Tab 管理控制 tab 的
 * 创建 {@link TabManager#createNewTab()}
 * 激活 {@link TabManager#setActiveTab(Tab)}
 * 移除 {@link TabManager#removeTab(Tab)}
 * 和当前tab的
 * 截图 {@link TabManager#capture2()}
 **/

public class TabManager {
    private static long sNextId = 1;

    /**
     * Maximum number of tabs.
     */
    private int mMaxTabs;
    /**
     * Private array of WebViews that are used as tabs.
     */
    private ArrayList<Tab> mTabs;
    /***
     * Queue of most recently viewed tabs.
     */
    private ArrayList<Tab> mTabQueue;
    /**
     * Current position in mTabs.
     */
    private int mCurrentTab = 0;


    private WindowUiController mHomeUiController;
    private UCTabAdapter mTabAdapter;

    public TabManager(Context context, WindowUiController controller) {
        mHomeUiController = controller;
        mMaxTabs = context.getResources().getInteger(R.integer.max_tab_count);
        mTabs = new ArrayList<>(mMaxTabs);
        mTabQueue = new ArrayList<>(mMaxTabs);
        mTabAdapter = new UCTabAdapter(context,controller);
    }

    public UCTabAdapter getTabAdapter(){
        return mTabAdapter;
    }

    synchronized static long getNextId() {
        return sNextId++;
    }


    /**
     * return the list of tabs
     */
    private List<Tab> getTabs() {
        return mTabs;
    }

    /**
     * Return the tab at the specified position.
     *
     * @return The Tab for the specified position or null if the tab does not
     * exist.
     */
    public Tab getTab(int position) {
        if (position >= 0 && position < mTabs.size()) {
            return mTabs.get(position);
        }
        return null;
    }

    /**
     * Return the current tab.
     *
     * @return The current tab.
     */
    public Tab getCurrentTab() {
        return getTab(mCurrentTab);
    }

    /**
     * Return the current tab position.
     *
     * @return The current tab position
     */
    public int getCurrentPosition() {
        return mCurrentTab;
    }

    /**
     * Given a Tab, find it's position
     *
     * @return position of Tab or -1 if not found
     */
    public int getTabPosition(Tab tab) {
        if (tab == null) {
            return -1;
        }
        return mTabs.indexOf(tab);
    }

    public boolean canCreateNewTab() {
        return mMaxTabs > mTabs.size();
    }

    public void createNewTab() {
        createNewTab(null);
    }

    private void createNewTab(Bundle state) {

        // Create a new tab and add it to the tab list
        if (!canCreateNewTab()) {
            return;
        }
        Tab t = new Tab(mHomeUiController, state);
        mTabs.add(t);
        setActiveTab(t);
        if (mHomeUiController != null) {
            mHomeUiController.onCreateTab(t);
            mHomeUiController.onTabCountChanged(getTabCount());
        }
    }

    public boolean removeTab(int index) {
        if (index < 0 || index >= getTabCount()) {
            return false;
        }
        return removeTab(mTabs.get(index));
    }

    /**
     * Remove the tab from the list. If the tab is the current tab shown, the
     * last created tab will be shown.
     *
     * @param t The tab to be removed.
     */
    private boolean removeTab(Tab t) {
        if (t == null) {
            return false;
        }

        // Grab the current tab before modifying the list.
        Tab current = getCurrentTab();

        // Remove t from our list of tabs.
        mTabs.remove(t);

        // Put the tab in the background only if it is the current one.
        if (current == t) {
            t.putInBackground();
            mCurrentTab = 0;
        } else {
            // If a tab that is earlier in the list gets removed, the current
            // index no longer points to the correct tab.
            mCurrentTab = getTabPosition(current);
            if (mCurrentTab >= getTabCount()) {
                mCurrentTab--;
            }
        }

        // destroy the tab
        t.destroy();

        // Remove it from the queue of viewed tabs.
        mTabQueue.remove(t);
        if (mHomeUiController != null) {
            mHomeUiController.onTabCountChanged(getTabCount());
        }
        return true;
    }

    /**
     * Returns the number of tabs created.
     *
     * @return The number of tabs created.
     */
    public int getTabCount() {
        return mTabs.size();
    }

    public void setActiveTab(Tab tab) {
        tab.putInForeground();
        mCurrentTab = mTabs.indexOf(tab);
    }

    public void capture2() {
        if (getCurrentTab() != null) {
            getCurrentTab().capture2();
        }
        mTabAdapter.updateData(getTabs());
    }
}
